<template>
  <NuxtLayout>
    <el-breadcrumb class="my-crumb">
      <el-breadcrumb-item v-for="(item, index) in crumbData" :key="index">
        <a :href="item.href">{{ item.title }}</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <NewsMenuTable :typeData="typeData" :typeSelectId="listForm.categoryId" :tableData="tableData" />
    <div class="pagin" v-show="tableData.length > 0">
      <el-pagination background layout="prev, pager, next, total" :total="total" :page-size="listForm.pageSize"
        @change="pageChangeFn" />
    </div>
  </NuxtLayout>
</template>

<script setup>
import { useCountStore } from '~/store/counter.js'
import { categoryPageList, informationPageList } from '~/server/api'
useCountStore().$patch({
  indexActive: 2,
});
const crumbData = ref([
  { title: '首页', href: '/' },
  { title: '新闻资讯', href: 'javascript:;' },
])
// TDK
const prevHost = useRequestHeaders()["x-forwarded-proto"];
const host = useRequestHeaders()["host"];
const fullPath = useRoute()["fullPath"];
useSeoMeta({
  title: "技考邦_新闻资讯",
  keywords: "技考邦,新闻资讯",
  description: "技考邦，新闻资讯。",
  ogTitle: "技考邦_新闻资讯",
  ogDescription: "技考邦，新闻资讯。",
  ogType: "article",
  ogUrl: `${prevHost}://${host}${fullPath}`,
});
// 获取类型
const typeData = ref([])
const { data: typeRes } = await useAsyncData('categoryPageList', () => useServerRequest(categoryPageList, {
  method: 'post',
  body: JSON.stringify({
    pageNum: 1,
    pageSize: 10,
    parentId: 0
  })
}))
if (typeRes.value && typeRes.value.errno === 0) {
  typeData.value = typeRes.value.result.list;
}
// 列表
const listForm = ref({
  pageNum: 1,
  pageSize: 20
})
const tableData = ref([])
const total = ref(0)
const getList = async () => {
  let { data: listRes } = await useAsyncData('informationPageList', () => useServerRequest(informationPageList, {
    method: 'post',
    body: JSON.stringify(listForm.value)
  }))
  if (listRes.value && listRes.value.errno === 0) {
    tableData.value = listRes.value.result.list;
    total.value = listRes.value.result.total;
    return false;
  }
}
getList()
// 分页
const pageChangeFn = (val) => {
  useRouter().push(`/news?p=${val}`)
}
// 监听
watchEffect(() => {
  // 分页
  if (useRoute().query.p) {
    listForm.value.pageNum = useRoute().query.p;
    getList()
  }
})
</script>

<style lang="less" scoped>
.pagin {
  width: 1200px;
  margin: 0 auto 30px;
  display: flex;
  justify-content: center;
}
</style>